package
{
    public class BackgroundCreator
    {
        import flash.display.*;
        import flash.geom.*;
        import flash.filters.*;

        import flash.display.Bitmap;
    
        
        public static function buildBackground(width:uint, height:uint):Bitmap
        {
            var data:BitmapData = new BitmapData(width, height,false, 0x000000); 
            var bitmap:Bitmap = new Bitmap(data);
            
            
            var sprite:Sprite = new Sprite();            
            
            sprite.graphics.beginFill(0xbdd8f5);
            sprite.graphics.drawRect(0,0,width,height);
            sprite.graphics.endFill();

            //Create a Matrix instance and assign the Gradient Box
            var matr:Matrix = new Matrix();
            matr.createGradientBox( 30, height*0.2, Math.PI /2, 0, height*0.8 );
            //SpreadMethod will define how the gradient is spread. Note!!! Flash uses CONSTANTS to represent String literals

            
            //sprite.graphics.beginFill(0x000000);
            sprite.graphics.beginGradientFill( GradientType.LINEAR, [ 0x305a8f, 0x000000 ], [ 1, 1 ], [ 0, 125 ], matr, SpreadMethod.PAD );            
            sprite.graphics.drawRect(0,height*0.8,width,height*0.2);
            sprite.graphics.endFill();
            
            var w:uint;
            var h:uint;
                        
            var pos:uint = 10* Math.random();                 
            for (;;) {
                w = 20+Math.random()*30;
                h = 30+Math.random()*250;
                drawBuilding(0x93c0e7, sprite.graphics, pos,height*0.8, w,h,0x93c0e7);
                pos+=1.7*Math.random()*w;
                if (pos>width) {
                    break;
                }
            }

            pos = 10* Math.random();            
            for (;;) {
                w = 20+Math.random()*40;
                h = 30+Math.random()*170;
                drawBuilding(0x527ead, sprite.graphics, pos,height*0.8, w,h,0x7092b8);
                pos+=1.5*Math.random()*w;
                if (pos>width) {
                    break;
                }
            }

            pos = 10* Math.random();           
            for (;;) {
                w = 20+Math.random()*50;
                h = 30+Math.random()*100;
                drawBuilding(0x4670a2, sprite.graphics, pos,height*0.8, w,h);
                pos+=1.2*Math.random()*w;
                if (pos>width) {
                    break;
                }
            }
            
            data.draw(sprite);

            return bitmap;            
        }
        
      
        private static function drawBuilding(color:Number, g:Graphics, x:uint, y:uint,width:uint, height:uint, 
                                                winColor:Number = 0xFFFFFF, marginColor:Number = 0x808080):void {

            g.lineStyle(1,color);
            var numSteps:uint = 1+Math.random()*15;
            var lastHeight:uint = height;
            var lastWidth:uint = width;
            var numWins:uint = width / 4;
            
            for (var i:uint=0; i<numSteps; i++) {
                g.beginFill(color);
                g.drawRect(x-lastWidth/2,y-lastHeight,lastWidth, lastHeight)
                g.endFill();
                
                
                
                lastWidth-=4* Math.random() * 2;
                lastHeight+=4* Math.random();
                if (lastWidth<1) {
                    break;
                }
            }
            
            for (var n:uint =0; n<numWins-1; n++) {
                for (var m:uint = 3; m<height/4; m++) {
                    if (Math.random()<0.2) {
                        g.beginFill(winColor);
                        g.drawRect(4+x-width/2+n*4, y-m*4, 2, 2);
                        g.endFill();
                    }
                }
            }
        }
        
    }
}
